package com.zq.server.web.exception;

import java.util.ArrayList;
import java.util.List;

import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.zq.common.constant.NumberConstants;
import com.zq.common.core.domain.Result;
import com.zq.common.enums.BusinessEnum;
import com.zq.common.exception.base.BaseException;

import lombok.extern.slf4j.Slf4j;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 处理自定义的业务异常
     *
     * @param
     * @param e
     */
    @ExceptionHandler(value = BaseException.class)
    public Result BusinessExceptionHandler(BaseException e) {
        log.error("发生业务异常！原因是：{}", e.getMessage());
//        e.printStackTrace();
        return Result.error("5000", e.getMessage());
    }

//    /**
//     * 系统异常
//     */
//    @ExceptionHandler(Exception.class)
//    public Result handleException(Exception e, HttpServletRequest request) {
//        String requestURI = request.getRequestURI();
//        log.error("请求地址'{}',发生系统异常.", requestURI, e);
//        return Result.error("10003",e.getMessage());
//    }

    /**
     * 处理空指针的异常
     *
     * @param
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public Result NullPointerException(NullPointerException e) {
        log.error("发生空指针异常！原因是:", e);
        return Result.error(BusinessEnum.NOT_FOUND);
    }



    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
    public Result methodArgumentNotValidExceptionHandler(Exception e) {
        log.error("参数校验异常！原因是:", e);
        List<FieldError> fieldErrors = null;
        if (e instanceof MethodArgumentNotValidException) {
            fieldErrors = ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors();
        }
        if (e instanceof BindException) {
            fieldErrors = ((BindException) e).getBindingResult().getFieldErrors();
        }
        if (fieldErrors == null) {
            return Result.error("1001", "参数错误");
        }
        if (fieldErrors.size() == NumberConstants.ONE_){
            return Result.error("1001", fieldErrors.get(0).getDefaultMessage());
        }
        List<String> defaultMessages = new ArrayList<>(fieldErrors.size());
        List<String> systemDefaultMessages = new ArrayList<>(fieldErrors.size());
        for (FieldError fieldError : fieldErrors) {
            defaultMessages.add(fieldError.getField() + ":" + fieldError.getDefaultMessage());
            systemDefaultMessages.add(fieldError.getDefaultMessage());
        }
        log.error(defaultMessages.toString());
        return Result.error("1001", String.join("-", systemDefaultMessages));
    }

}
